package com.myspace.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.myspace.sdk.MSRequest;
import com.myspace.sdk.MSSDK;
import com.myspace.sdk.MSRequest.MSRequestCallback;
import com.myspace.util.MSDrawableManager;

public class MSStatusMoodActivity extends MSBaseActivity {

	private TextView mCurrentStatusText;
	private TextView mCurrentMoodText;
	private ImageView mCurrentMoodImage;
	private EditText mNewStatusText;
	private String mNewMoodName;
	private Button mUpdateButton;
	private Button mShowAllButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setPageName(R.string.PAGE_TITLE_STATUS_MOOD);
		super.setMainView(R.layout.statusmood);

		getStatusMood();

		LinearLayout layout = (LinearLayout) findViewById(R.id.currentStatusLinearLayout);
		layout.setBackgroundColor(Color.LTGRAY);

		mNewStatusText = (EditText)findViewById(R.id.statusEditText);

		mUpdateButton = (Button) findViewById(R.id.setStatusButton);
		mUpdateButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				updateStatusMood();
			}
		});
		
		mShowAllButton = (Button) findViewById(R.id.statusShowAllButton);
		mShowAllButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				gotoPage(MSStatusMoodListActivity.class);
			}
		});
		
		getMoods();
	}

	private void getStatusMood() {
		Map<String, String> queryParams = new HashMap<String, String>();
		queryParams.put("fields", "author,recentComments,source");
		
		MSSDK.getCurrentStatusMoodWithParameters(queryParams, new GetStatusMood_MSRequestCallback());
	}

	private void updateStatusMood() {
		
		Map<String, Object> params = new HashMap<String, Object>();
		
		String status = mNewStatusText.getText().toString();
		if(status != null && !status.equalsIgnoreCase("")) {
			params.put("status", status);
		}

		if(mNewMoodName != null) {
			params.put("moodName", mNewMoodName);	
		}
		
		if(params.size() > 0) {
			try {
				MSSDK.setStatusMood(params, new SetStatusMood_MSRequestCallback());
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			showToast("Please Enter New Status or Mood");
		}
	}

	private void getMoods() {
		MSSDK.getMoods(new GetMoods_MSRequestCallback());
	}

	private void updateView(StatusMoodItem item) {
		mCurrentStatusText = (TextView) findViewById(R.id.statusText);
		mCurrentStatusText.setText(item.getStatus());
		
		mCurrentMoodText = (TextView) findViewById(R.id.moodText);
		mCurrentMoodText.setText(item.getMoodName());
		
		if(item.getMoodImageUrl() != null) {
			mCurrentMoodImage = (ImageView) findViewById(R.id.moodImage);
			MSDrawableManager.getInstance().fetchDrawableOnThread(item.getMoodImageUrl(), mCurrentMoodImage);
		}
	}
	
	private void updateView(List<MoodItem> items) {
		Spinner moodSpinner = (Spinner) findViewById(R.id.moodSpinner);    
		moodSpinner.setAdapter(new MoodSpinnerAdapter(this, items));
		moodSpinner.setOnItemSelectedListener(new MoodSelectedListener());
	}
	
	private class MoodSelectedListener implements OnItemSelectedListener {    
		public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
			mNewMoodName = (String) ((TextView)view).getText();    
		}    

		public void onNothingSelected(AdapterView<?> parent) {  
		}
	}
	
	private class MoodSpinnerAdapter extends BaseAdapter {
		private Context mContext;
		private List<MoodItem> mItems;

		public MoodSpinnerAdapter(Context c, List<MoodItem> items) {
			mContext = c;
			mItems = items;
		}

		public int getCount() {
			return mItems.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			TextView v;
			if (convertView == null) { 
				v = new TextView(mContext);
				v.setHeight(30);
				v.setPadding(10, 5, 10, 0);
				v.setTextColor(Color.BLACK);
			} else {
				 v = (TextView) convertView;
			}
			
			MoodItem item = mItems.get(position);
			v.setText(item.getMoodName());

			return v;
		}
	}
	
	private class GetStatusMood_MSRequestCallback extends MSRequestCallback {

		public void requestDidFail(MSRequest request, Throwable error) {
			showToast(error.toString());
		}

		public void requestDidLoad(MSRequest request, Object result) {
			Map<?,?> data = (Map<?,?>)result;
			result  = data.get("data");
			
			if(result instanceof Map<?,?>) {
				Map<?,?> statusObject = (Map<?,?>) result;
				String status = (String)statusObject.get("status");
				String mood = (String)statusObject.get("moodName");
				String moodImageUrl = (String)statusObject.get("moodImageURL");
				
				StatusMoodItem item = new StatusMoodItem(status, mood, moodImageUrl);
				updateView(item);
			}
		}
	}

	private class GetMoods_MSRequestCallback extends MSRequestCallback {

		public void requestDidFail(MSRequest request, Throwable error) {
			showToast(error.getMessage());
		}

		public void requestDidLoad(MSRequest request, Object result) {
			Map<?,?> data = (Map<?,?>)result;
			result  = data.get("data");
			
			if(result instanceof List<?>) {
				List<MoodItem> items = new ArrayList<MoodItem>();
				List<?> moodList = (List<?>) result;
				for (int i=0; i < moodList.size(); i++) {
					Map<?,?> moodObject = (Map<?,?>) moodList.get(i);
					String mood = (String)moodObject.get("moodName");
					String moodImageUrl = (String)moodObject.get("moodImageURL");
					
					MoodItem item = new MoodItem(mood, moodImageUrl);
					items.add(item);
				}	
				updateView(items);
			}
		}
	}

	private class SetStatusMood_MSRequestCallback extends MSRequestCallback {

		public void requestDidFail(MSRequest request, Throwable error) {
			showToast(error.getMessage());
		}

		public void requestDidLoad(MSRequest request, Object result) {
			mNewStatusText.setText("");
			getStatusMood();
		}
	}

	private class StatusMoodItem {
		private String mStatus;
		private String mMoodName;  
		private String mMoodImageUrl;
		
 		public StatusMoodItem(String status, String moodName, String moodImageUrl) {
			mStatus = status;
			mMoodName = moodName;
			mMoodImageUrl = moodImageUrl;
		}

		public String getStatus() {
			return mStatus;
		}

		public String getMoodName() {
			return mMoodName;
		}
		
		public String getMoodImageUrl() {
			return mMoodImageUrl;
		}
	}

	private class MoodItem {
		private String mMoodName;  
		
		public MoodItem(String moodName, String moodImageUrl) {
			mMoodName = moodName;
		}

		public String getMoodName() {
			return mMoodName;
		}
	}
}